/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

package ru.teleology.cubefs.editor.hex;

import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.border.LineBorder;

public class HexTableCellEditor extends DefaultCellEditor {

	private static final long serialVersionUID = -4085432972668324958L;
	Class[] argTypes = new Class[]{String.class};
	java.lang.reflect.Constructor constructor;
	Object value;

	public HexTableCellEditor() {
		super(new HexField());
		getComponent().setName("Table.editor");
	}

	public boolean stopCellEditing() {
		String s = (String)super.getCellEditorValue();
		// Here we are dealing with the case where a user
		// has deleted the string value in a cell, possibly
		// after a failed validation. Return null, so that
		// they have the option to replace the value with
		// null or use escape to restore the original.
		// For Strings, return "" for backward compatibility.
		if ("".equals(s)) {
			if (constructor.getDeclaringClass() == String.class) {
				value = s;
			}
			super.stopCellEditing();
		}

		try {
			value = constructor.newInstance(new Object[]{s});
		}
		catch (Exception e) {
			((JComponent)getComponent()).setBorder(new LineBorder(Color.red));
			return false;
		}
		return super.stopCellEditing();
	}

	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected,
			int row, int column) {
		this.value = null;
		((JComponent)getComponent()).setBorder(new LineBorder(Color.black));
		try {
			Class type = table.getColumnClass(column);
			// Since our obligation is to produce a value which is
			// assignable for the required type it is OK to use the
			// String constructor for columns which are declared
			// to contain Objects. A String is an Object.
			if (type == Object.class) {
				type = String.class;
			}
			constructor = type.getConstructor(argTypes);
		}
		catch (Exception e) {
			return null;
		}
		return super.getTableCellEditorComponent(table, value, isSelected, row, column);
	}

	public Object getCellEditorValue() {
		return value;
	}
}
